package com.yang.dao;

import com.yang.pojo.InfoDetail;
import com.yang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InfoDetailDao {
    //根据类目id查询选项信息
    public ArrayList<InfoDetail> selectInfoDetailByBasicInfoId(int basicInfoId) throws SQLException {
        ArrayList<InfoDetail> infoDetails = new ArrayList<>();
        String sql = "SELECT info_detail_id infoDetailId,info_detail_name infoDetailName,info_detail_desc infoDetailDesc,fk_basic_info_id fkBasicInfoId FROM tb_info_detail,tb_basic_info WHERE tb_info_detail.fk_basic_info_id=tb_basic_info.basic_info_id AND basic_info_id = ?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        infoDetails = (ArrayList<InfoDetail>) queryRunner.query(sql, new BeanListHandler<InfoDetail>(InfoDetail.class),basicInfoId);
        return infoDetails;
    }

    //保存评估选项信息
    public int insertInfoDetail(String infoDetailName,String infoDetailDesc,int fkBasicInfoId) throws SQLException {
        int i=0;
        String sql = "INSERT INTO tb_info_detail(info_detail_name,info_detail_desc,fk_basic_info_id) VALUES(?,?,?)";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql, infoDetailName, infoDetailDesc, fkBasicInfoId);
        return i;
    }

    //根据id查询评估选项信息
    public InfoDetail selectInfoDetailById(int infoDetailId) throws SQLException {
        String sql = "SELECT info_detail_id infoDetailId,info_detail_name infoDetailName,info_detail_desc infoDetailDesc,fk_basic_info_id fkBasicInfoId FROM tb_info_detail where info_detail_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        InfoDetail infoDetail = queryRunner.query(sql, new BeanHandler<InfoDetail>(InfoDetail.class), infoDetailId);
        return infoDetail;
    }

    //根据id更新评估选项信息
    public int updateInfoDetailById(String infoDetailName,String infoDetailDesc,int infoDetailId) throws SQLException {
        int i = 0;
        String sql = "UPDATE tb_info_detail SET info_detail_name=?,info_detail_desc=? WHERE info_detail_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql, infoDetailName, infoDetailDesc, infoDetailId);
        return i;
    }

    //根据id删除评估选项
    public int deleteInfoDetailById(int infoDetailId) throws SQLException {
        int i=0;
        String sql = "DELETE FROM tb_info_detail WHERE info_detail_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql, infoDetailId);
        return i;
    }
}
